package com.qf.dao.impl;

import com.qf.dao.UserDao;
import com.qf.pojo.TbUser;
import com.qf.utils.DBUtils;
import com.qf.utils.SqlUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    private QueryRunner queryRunner = new QueryRunner();
    @Override
    public void addUser(TbUser user) {

        String sql = SqlUtils.getSql("add");
        Object[] a = {user.getUsername(),user.getPassword(),user.getNickname(),user.getEmail(),user.getGender(),user.getCode()};
        try {
             queryRunner.update(DBUtils.getConnection(), sql, a);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int active(String username, String code) throws Exception {
       String sql = SqlUtils.getSql("userselect");
       int result=queryRunner.update( DBUtils.getConnection(),sql,username,code);
        return result;
    }

    @Override
    public TbUser findUserByName(String username) throws Exception {
        String sql = SqlUtils.getSql("usernam");
        return queryRunner.query( DBUtils.getConnection(),sql,new BeanHandler<>(TbUser.class),username);
    }
}
